﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using TmU220PrintHelperV2d0.Entity;
using TmU220PrintHelperV2d0.HttpHelper;
using TmU220PrintHelperV2d0.Service;
using ZhangLogSysV1d0;

namespace TmU220PrintHelperV2d0.Singleton
{
    public class PrinterInfoSetSingletonV2d0
    {
        private static PrinterInfoSetSingletonV2d0 _instance = null;
        private PrinterInfoSetV2d0 _printerInfoSet = null;
        private static readonly object syslock = new object();
        /// <summary>
        /// 单例模式
        /// </summary>
        public static PrinterInfoSetSingletonV2d0 Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (syslock)
                    {
                        if (_instance == null)
                        {
                            _instance = new PrinterInfoSetSingletonV2d0();
                        }
                    }
                }
                return _instance;
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        private PrinterInfoSetSingletonV2d0()
        {
            _printerInfoSet = new PrinterInfoSetV2d0();
            LoadData();
        }
        /// <summary>
        /// 装载数据
        /// </summary>
        private void LoadData()
        {
            try
            {
                string shopId = SysCfgInfoSingletonV1d0.Instance.GetShopId().ToString();
                //web端用的接口
                Type currType = typeof(rms_printer);
                string currClassName = currType.Name;
                //表名称 内部约定，类名称就是表名称
                string tableName = currClassName;
                string sqlString = string.Format("select * from rms_printer where shop_id={0} and is_active=1", shopId);
                //使用接口单例模式读取数据
                string jsonString = GetDataBySQLSingletonV1d0.Instance.Run(sqlString);
                //jsonString = GetDataSingletonV1d0.Instance.Run("rms_printer");
                //解析 反序列化
                List<rms_printer> printerList = JsonConvert.DeserializeObject<List<rms_printer>>(jsonString);
                //过滤ip地址正常的
                _printerInfoSet.PrinterInfoList = printerList.FindAll(x =>
                {
                    return HotchPotchV2d0.IsIP(x.ip);
                });
            }
            catch(Exception ex )
            {
                SimpleLoger.Instance.Error("打印机配置信息下载失败！！！");
            }

        }
        public PrinterInfoSetV2d0 GetPrinterInfoSet()
        {
            return _printerInfoSet;
        }
        public string GetDeviceTypeByPrinterName(string printerName)
        {
            var printerInfo = _printerInfoSet.PrinterInfoList.Find(x => x.printername == printerName);
            if(printerInfo!=null) return printerInfo.device_type;
            else return "TM-U220";
        }

    }
}
